﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace WebSite_CodeMan.BLL
{
    public class LeaveInfo
    {
        /// <summary>
        /// 分页获取内容数据信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageCount"></param>
        /// <param name="RecordCount"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public static DataTable GetAllLeaveInfo(int PageIndex, int PageSize, out int PageCount, out int RecordCount, string strWhere)
        {
            try
            {
                string strSql = " SELECT T.*,C.T_ClassEnName FROM " +
                                " (SELECT L.T_LeaveID,L.T_NoteID,L.T_LeaveType,L.T_LeaveNote,L.T_LeaveIP,L.T_DateTime,L.T_Flag,N.T_NoteTitle,N.T_NoteClassID,N.T_NoteImage " +
                                " FROM T_Leave_Info L LEFT JOIN T_Note_Info N  ON L.T_NoteID=N.T_NoteID ) AS T LEFT JOIN T_Note_Class C "+
                                " ON T.T_NoteClassID=C.T_NoteClassID ";
                return DBUnlity.AccessHelper.ExecutePager(PageIndex, PageSize, "T_LeaveID", "*", strSql, strWhere, " T_Flag ASC,T_LeaveID DESC ", out PageCount, out RecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 分页获取内容数据信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageCount"></param>
        /// <param name="RecordCount"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public static DataTable GetAllBaseLeaveInfo(int PageIndex, int PageSize, out int PageCount, out int RecordCount, string strWhere)
        {
            try
            {
                string strSql = " SELECT * FROM T_Leave_Info";
                return DBUnlity.AccessHelper.ExecutePager(PageIndex, PageSize, "T_LeaveID", "*", strSql, strWhere, " T_LeaveID DESC ", out PageCount, out RecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条评论信息
        /// </summary>
        /// <returns></returns>
        public static OleDbDataReader GetAllLeaveInfo(string strlID)
        {
            try
            {
                return DBUnlity.AccessHelper.ExecuteReader(DBUnlity.AccessHelper.ConnectionString, string.Format(" SELECT T_LeaveNote,T_LeaveReBack FROM T_Leave_Info WHERE T_LeaveID= {0} ", strlID), null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个内容信息下的所有评论信息
        /// </summary>
        /// <returns></returns>
        public static OleDbDataReader GetAllLeaveByNoteInfo(string strTop,string strNID)
        {
            try
            {
                return DBUnlity.AccessHelper.ExecuteReader(DBUnlity.AccessHelper.ConnectionString, string.Format(" SELECT {1}  * FROM T_Leave_Info WHERE T_NoteID= {0} AND T_Flag=1 ", strNID, (string.IsNullOrEmpty(strTop) ? "" : " TOP " + strTop)), null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除内容信息
        /// </summary>
        /// <param name="strClassID"></param>
        /// <returns></returns>
        public static bool DelLeaveMes(string strLeaveID)
        {
            return (DBUnlity.AccessHelper.ExecuteNonQuery(DBUnlity.AccessHelper.ConnectionString, string.Format(" DELETE FROM T_Leave_Info WHERE T_LeaveID IN ({0})  ", strLeaveID), null) > 0) ? true : false;
        }
        /// <summary>
        /// 回复内容信息
        /// </summary>
        /// <param name="strClassID"></param>
        /// <returns></returns>
        public static bool BackLeaveMes(string strLeaveID,string strReNote)
        {
            return (DBUnlity.AccessHelper.ExecuteNonQuery(DBUnlity.AccessHelper.ConnectionString, string.Format(" UPDATE  T_Leave_Info SET T_LeaveReBack='{0}' , T_Flag=1 WHERE T_LeaveID ={1}  ", strReNote, strLeaveID), null) > 0) ? true : false;
        }

        /// <summary>
        /// 获取该信息的评论信息的数量
        /// </summary>
        /// <param name="strNID"></param>
        /// <returns></returns>
        public static string GetLeaveCount(string strNID)
        {
            try
            {
                return DBUnlity.AccessHelper.ExecuteScalar(DBUnlity.AccessHelper.ConnectionString, string.Format("SELECT COUNT(*) FROM T_Leave_Info WHERE T_NoteID={0} ",strNID), null).ToString();
            }
            catch
            {
                return "0";
            }
        }
        
        /// <summary>
        /// 添加评论信息
        /// </summary>
        /// <param name="strNID"></param>
        /// <param name="strType"></param>
        /// <param name="strLeaveNote"></param>
        /// <param name="strName"></param>
        /// <param name="strSex"></param>
        /// <param name="strTel"></param>
        /// <param name="strEmail"></param>
        /// <returns></returns>
        public static bool AddLeaveMes(string strNID,string strType, string strLeaveNote,string strName,string strSex,string strTel,string strEmail)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_NoteID",strNID),
                                          new OleDbParameter("@T_LeaveType",strType),
                                          new OleDbParameter("@T_LeaveNote",strLeaveNote),
                                          new OleDbParameter("@T_LeaveIP",Common.GetMes.GetIp()),
                                          new OleDbParameter("@T_DateTime",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                                          new OleDbParameter("@T_Name",strName),
                                          new OleDbParameter("@T_Sex",strSex),
                                          new OleDbParameter("@T_Tel",strTel),
                                          new OleDbParameter("@T_Email",strEmail)
                                        };
                return (DBUnlity.AccessHelper.ExecuteNonQuery(DBUnlity.AccessHelper.ConnectionString, " INSERT INTO T_Leave_Info(T_NoteID,T_LeaveType,T_LeaveNote,T_LeaveIP,T_DateTime,T_Name,T_Sex,T_Tel,T_Email) VALUES(@T_NoteID,@T_LeaveType,@T_LeaveNote,@T_LeaveIP,@T_DateTime,@T_Name,@T_Sex,@T_Tel,@T_Email)", parm) > 0) ? true : false;
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }
    }
}
